Imports ColorProgressBar
Imports BusinessLogicLayer.BLMain
Imports BusinessLogicLayer.BLException
Imports DataAccessLayer
Imports UniversalLayer

Friend NotInheritable Class frmLogin

    ' TODO: Insert code to perform custom authentication using the provided username and password 
    ' (See http://go.microsoft.com/fwlink/?LinkId=35339).  
    ' The custom principal can then be attached to the current thread's principal as follows: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' where CustomPrincipal is the IPrincipal implementation used to perform authentication. 
    ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
    ' such as the username, display name, etc.

    Dim dtUserDetails As DataTable
    Friend iAnimationDuration, iTransparencyDuration, iTickTransitions As Integer
    Dim iFormEvents As eFormEvents = eFormEvents.Loading
    Friend bDisableLoginControls As Boolean
    Friend dHeight(1) As Integer, dWidth(1) As Integer
    Friend dtSettingsMain As DataTable

    Friend Enum eFormEvents
        Loading = 1
        Closing = 0
    End Enum

    ''' <summary>
    ''' Operation
    ''' </summary>
    ''' <remarks></remarks>
    Friend Enum eAccessType
        ''' <summary>
        ''' Login screen
        ''' </summary>
        ''' <remarks></remarks>
        Login = 0
        ''' <summary>
        ''' New user access request
        ''' </summary>
        ''' <remarks></remarks>
        Request = 1
        ''' <summary>
        ''' Forgotten password retrieval
        ''' </summary>
        ''' <remarks></remarks>
        Retrieve = 2
        ''' <summary>
        ''' Default password change
        ''' </summary>
        ''' <remarks></remarks>
        ChangeDefault = 3
    End Enum

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim _DLReader As DLReader
        Dim _DLSettings As DLSettings
        Try
            _DLReader = New DLReader("frmLogin")
            _DLSettings = New DLSettings
            dtSettingsMain = _DLSettings.FetchAllSettings 'CType(_DLSettings.FetchSettings("", "", "", DLSettings.eSettingsReturnType.DataTable), DataTable)
            _DLReader.FetchUserDetails(dtUserDetails, "")

            dHeight(0) = Me.Height
            dHeight(1) = Me.Height
            dWidth(0) = Me.Width
            dWidth(1) = Me.Width

            iTickTransitions = CInt(DLSettings.FetchOneSetting(dtSettingsMain, "Login", "Transitions"))
            iAnimationDuration = CInt(DLSettings.FetchOneSetting(dtSettingsMain, "Login", "AnimationDuration"))
            iTransparencyDuration = CInt(DLSettings.FetchOneSetting(dtSettingsMain, "Login", "TransparencyDuration"))
            bDisableLoginControls = CBool(DLSettings.FetchOneSetting(dtSettingsMain, "Login", "DisableLoginControls"))
        Finally
            _DLReader = Nothing
            _DLSettings = Nothing
        End Try
    End Sub

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator
            _GUIPopulator.InitializeFormLogin(Me)
            Me.StartPosition = FormStartPosition.CenterScreen
            Me.TopMost = True
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.frmLogin_Load")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

#Region "Login"

    Private Sub txtUserName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtUserName.TextChanged
        'Dim _BLMain As BusinessLogicLayer.BLMain
        Try
            '_BLMain = New BusinessLogicLayer.BLMain(BusinessLogicLayer.BLMain.eInitMode.Validation)
            'btnOK.Enabled = Not (IsEmpty(txtUserName.Text) OrElse IsEmpty(txtPassword.Text))
            GUIPopulator.AllowAccess(Me, eAccessType.Login, True)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.txtUserName_TextChanged")
        Finally
            '_BLMain = Nothing
        End Try
    End Sub

    Private Sub txtPassword_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPassword.TextChanged
        'Dim _BLMain As BusinessLogicLayer.BLMain
        Try
            '_BLMain = New BusinessLogicLayer.BLMain(BusinessLogicLayer.BLMain.eInitMode.Validation)
            'btnOK.Enabled = Not (IsEmpty(txtUserName.Text) OrElse IsEmpty(txtPassword.Text))
            GUIPopulator.AllowAccess(Me, eAccessType.Login, True)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.txtPassword_TextChanged")
        Finally
            '_BLMain = Nothing
        End Try
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim _BLMain As BusinessLogicLayer.BLMain
        Dim _DLSettings As DLSettings
        Dim _DLWriter As DLWriter
        Dim sMessage As String
        Dim dtLogin As DataTable
        Dim bValidLogin As Boolean
        Dim sApprovalState As String
        Dim sApprovalComments As String

        Try
            _BLMain = New BusinessLogicLayer.BLMain(BusinessLogicLayer.BLMain.eInitMode.Validation)
            _DLSettings = New DLSettings
            _DLWriter = New DLWriter("frmLogin")
            dtLogin = New DataTable
            Static iRemainingAttempts As Integer = CInt(DLSettings.FetchOneSetting(dtSettingsMain, "UserMgmt", "AllowedLoginAttempts"))

            If Not _BLMain.IsExistingUser(txtUserName.Text, dtUserDetails) Then
                MessageBox.Show("User does not exists..     ", "Invalid user", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If

            bValidLogin = _BLMain.ValidateCredentials(dtLogin, txtUserName.Text, txtPassword.Text)
            sApprovalState = dtLogin.Rows(0).Item("ApprovalState").ToString
            sApprovalComments = dtLogin.Rows(0).Item("Comments").ToString

            Select Case sApprovalState.ToUpper
                Case eApprovalType.Pending.ToString.ToUpper
                    MessageBox.Show("Your request is still " & sApprovalState.ToLower & " with the administrator!" & vbTab & vbCrLf & "Please try again later..", "Access", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Case eApprovalType.Revoked.ToString.ToUpper
                    MessageBox.Show("Your access has been " & sApprovalState & " with comments:" & vbCrLf & sApprovalComments, _
                                    "Access", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Case eApprovalType.Rejected.ToString.ToUpper
                    MessageBox.Show("Your access has been " & sApprovalState & " with comments:" & vbCrLf & sApprovalComments, _
                                    "Access", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Case eApprovalType.Approved.ToString.ToUpper, eApprovalType.Admin.ToString.ToUpper
                    If Not bValidLogin Then
                        bValidLogin = Nothing
                        iRemainingAttempts -= 1

                        If iRemainingAttempts = 0 Then
                            sMessage = "You have exceeded the permissible number of unsuccessful hits." & vbCrLf & _
                                           "Program will now exit.."
                            MessageBox.Show(sMessage, "Login error..", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                            End
                        End If

                        sMessage = "User does not exists or wrong password entered!! " & vbCrLf & _
                                    "Login window will close after 3 unsuccesful attempts (" & iRemainingAttempts.ToString & " remaining)." & vbCrLf & vbCrLf & _
                                    "Please use the ""Request Access"" or ""Forgot Password"" options accordingly"
                        Select Case MessageBox.Show(sMessage, "Login error..", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                            Case Windows.Forms.DialogResult.Retry : txtPassword.Text = ""
                            Case Windows.Forms.DialogResult.Cancel : End
                        End Select

                        If CStr(dtLogin.Rows(0).Item("NeedsPswdChng")) = "0" Then : CloseMe()
                        Else
                            pnlLogin.Visible = False
                            grpChngDefPswd.Visible = True
                            grpChngDefPswd.Top = 24
                            grpChngDefPswd.Left = 166
                        End If

                    Else
                        If sApprovalState.ToUpper = eApprovalType.Admin.ToString.ToUpper Then _
                            MessageBox.Show("WELCOME Administrator..", "Hello!")
                        _DLWriter.IncrementLoginCount(CInt(dtLogin.Rows(0).Item("PKey")))
                        CloseMe()
                    End If
            End Select

        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.btnOK_Click")
        Finally
            _BLMain = Nothing
            _DLSettings = Nothing
            _DLWriter = Nothing
            sMessage = Nothing
            dtLogin = Nothing
            bValidLogin = Nothing
            sApprovalState = Nothing
            sApprovalComments = Nothing
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        End
    End Sub

#End Region

#Region "Change Default Password"

#End Region

#Region "Password Retrieval"

    Private Sub lnkForgotPassword_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkForgotPassword.LinkClicked
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator
            pnlLogin.Enabled = Not bDisableLoginControls
            dHeight(0) = Me.Height : dHeight(1) = 223 : dWidth(0) = Me.Width : dWidth(1) = 785
            _GUIPopulator.AnimateResize(Me)
            '_GUIPopulator.ResetPasswordRetrieval(Me)
            '_GUIPopulator.ShowPasswordRetrieval(Me, True)
            ''_GUIPopulator.HideAccessRequest(Me, True)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.lnkForgotPassword_LinkClicked")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub txtResetUserName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtResetUserName.TextChanged
        Try
            'btnRetrievePassword.Enabled = Not Equals(txtResetUserName.Text.Length, 0) And _
            '                              Not Equals(cmbResetSecQues.SelectedIndex, -1) And _
            '                              Not Equals(txtResetSecAns.Text.Length, 0)
            GUIPopulator.AllowAccess(Me, eAccessType.Retrieve, True)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.txtResetUserName_TextChanged")
        End Try
    End Sub

    Private Sub cmbResetSecQues_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbResetSecQues.SelectedIndexChanged
        Try
            'btnRetrievePassword.Enabled = Not Equals(txtResetUserName.Text.Length, 0) And _
            '                              Not Equals(cmbResetSecQues.SelectedIndex, -1) And _
            '                              Not Equals(txtResetSecAns.Text.Length, 0)
            GUIPopulator.AllowAccess(Me, eAccessType.Retrieve, True)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.cmbResetSecQues_SelectedIndexChanged")
        End Try
    End Sub

    Private Sub txtResetSecAns_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtResetSecAns.TextChanged
        Try
            'btnRetrievePassword.Enabled = Not Equals(txtResetUserName.Text.Length, 0) And _
            '                              Not Equals(cmbResetSecQues.SelectedIndex, -1) And _
            '                              Not Equals(txtResetSecAns.Text.Length, 0)
            GUIPopulator.AllowAccess(Me, eAccessType.Retrieve, True)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.txtResetSecAns_TextChanged")
        End Try
    End Sub

    Private Sub btnRetrievePassword_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetrievePassword.Click
        Dim sMessage As String
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Dim _BLMain As BusinessLogicLayer.BLMain
        Dim _DLSettings As DLSettings
        Dim bValidRetrievalDetails As Boolean

        Try
            _DLSettings = New DLSettings
            _GUIPopulator = New EntertainmentManager.GUIPopulator
            _DLSettings = New DLSettings
            _BLMain = New BusinessLogicLayer.BLMain(BusinessLogicLayer.BLMain.eInitMode.Validation)
            bValidRetrievalDetails = _BLMain.ResetPassword(txtResetUserName.Text, cmbResetSecQues.Text, txtResetSecAns.Text)

            Static iAllowedAttempts As Integer = CInt(DLSettings.FetchOneSetting(dtSettingsMain, "UserMgmt", "AllowedRetrieveAttempts"))
            Static iRemainingAttempts As Integer = iAllowedAttempts

            If Not bValidRetrievalDetails Then
                bValidRetrievalDetails = Nothing
                iRemainingAttempts -= 1

                If iRemainingAttempts < 0 Then
                    sMessage = "You have exceeded the permissible number of unsuccessful hits." & vbCrLf & _
                               "Program will now exit.."
                    MessageBox.Show(sMessage, "Login error..", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                    End
                    End If

                sMessage = "User does not exists or wrong security details entered!! " & vbCrLf & _
                            "Login window will close after " & iAllowedAttempts.ToString & " unsuccesful attempts (" & iRemainingAttempts.ToString & " remaining)." & vbCrLf & vbCrLf & _
                            "Please use the ""Request Access"" or try again.."
                    Select Case MessageBox.Show(sMessage, "Security error..", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                        Case Windows.Forms.DialogResult.Abort : End
                        Case Windows.Forms.DialogResult.Retry : txtResetSecAns.Text = ""
                        Case Windows.Forms.DialogResult.Ignore
                            dHeight(0) = Me.Height : dHeight(1) = 223 : dWidth(0) = Me.Width : dWidth(1) = 436
                            _GUIPopulator.AnimateResize(Me)
                            '_GUIPopulator.HidePasswordRetrieval(Me, True)
                            _GUIPopulator = Nothing
                    End Select
                Else
                    dHeight(0) = Me.Height : dHeight(1) = 223 : dWidth(0) = Me.Width : dWidth(1) = 436
                    _GUIPopulator.AnimateResize(Me)
                    '_GUIPopulator.HidePasswordRetrieval(Me, True)
                    _GUIPopulator = Nothing

                    sMessage = "Enter your credentials to login.." & vbCrLf & "Remember to change the default password!"
                    MessageBox.Show(sMessage, "Login")
                End If

        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.btnRetrievePassword_Click")
        Finally
            sMessage = Nothing
            _BLMain = Nothing
            _DLSettings = Nothing
            _GUIPopulator = Nothing
            bValidRetrievalDetails = Nothing
        End Try
    End Sub

    Private Sub btnCancelRetrieval_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelRetrieval.Click
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator
            pnlLogin.Enabled = True
            dHeight(0) = Me.Height : dHeight(1) = 223 : dWidth(0) = Me.Width : dWidth(1) = 436
            _GUIPopulator.AnimateResize(Me)
            '_GUIPopulator.HidePasswordRetrieval(Me, True)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.btnCancelRetrieval_Click")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

#End Region

#Region "Request Access"

    Private Sub lnkRequestAccess_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkRequestAccess.LinkClicked
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator
            pnlLogin.Enabled = Not bDisableLoginControls
            dHeight(0) = Me.Height : dHeight(1) = 447 : dWidth(0) = Me.Width : dWidth(1) = 436
            _GUIPopulator.AnimateResize(Me)
            '_GUIPopulator.ResetAccessRequest(Me)
            '_GUIPopulator.ShowAccessRequest(Me, True)
            ''_GUIPopulator.HidePasswordRetrieval(Me)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.lnkRequestAccess_LinkClicked")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub txtNewUserName_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNewUserName.LostFocus
        'Dim _BLMain As BusinessLogicLayer.BLMain
        'Dim bExistingUser As Boolean
        Try
            '_BLMain = New BusinessLogicLayer.BLMain(BusinessLogicLayer.BLMain.eInitMode.Validation)
            'bExistingUser = _BLMain.IsExistingUser(txtNewUserName.Text, dtUserDetails)
            'picExistingUser.Visible = bExistingUser
            'btnRequestAccess.Enabled = bExistingUser
            GUIPopulator.AllowAccess(Me, eAccessType.Request, True)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.txtNewUserName_LostFocus")
        Finally
            'bExistingUser = Nothing
            '_BLMain = Nothing
        End Try
    End Sub

    Private Sub txtNewUserName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNewUserName.TextChanged
        'Dim _BLMain As BusinessLogicLayer.BLMain
        'Dim bExistingUser As Boolean
        Try
            '_BLMain = New BusinessLogicLayer.BLMain(BusinessLogicLayer.BLMain.eInitMode.Validation)
            'bExistingUser = _BLMain.IsExistingUser(txtNewUserName.Text, dtUserDetails)
            'picExistingUser.Visible = bExistingUser And Not Equals(txtNewUserName.Text.Length, 0)
            'btnRequestAccess.Enabled = Not Equals(txtNewUserName.Text.Length, 0) Or Not bExistingUser
            'btnRequestAccess.Enabled = Not Equals(txtNewUserName.Text.Length, 0) And _
            '                           Not picExistingUser.Visible And Not picNewPasswordMismatch.Visible And _
            '                           Not Equals(cmbNewSecQues.Text.Length, 0) And Not Equals(txtNewSecAns.Text.Length, 0)
            GUIPopulator.AllowAccess(Me, eAccessType.Request, True)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.txtNewUserName_TextChanged")
        Finally
            'bExistingUser = Nothing
            '_BLMain = Nothing
        End Try
    End Sub

    Private Sub txtNewPassword_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNewPassword.TextChanged
        'Dim bIsPswdStrong As Boolean = False
        'Dim bPasswordMatched As Boolean = Equals(txtNewPassword.Text, txtNewPasswordRetype.Text)
        'Dim _BLMain As BusinessLogicLayer.BLMain
        'Dim _DLSettings As DLSettings
        'Dim dPswdStrength As Double = 0.0

        Try
            '_BLMain = New BusinessLogicLayer.BLMain(BusinessLogicLayer.BLMain.eInitMode.Validation)
            '_DLSettings = New DLSettings
            'bIsPswdStrong = _BLMain.IsPasswordStrong(txtNewPassword.Text, dPswdStrength)
            'progPasswordStrength.Value = CInt(progPasswordStrength.Minimum + dPswdStrength * (progPasswordStrength.Maximum - progPasswordStrength.Minimum))
            'picNewPasswordMismatch.Visible = Not bPasswordMatched
            'btnRequestAccess.Enabled = bPasswordMatched And bIsPswdStrong
            'btnRequestAccess.Enabled = Not Equals(txtNewUserName.Text.Length, 0) And _
            '                           Not picExistingUser.Visible And Not picNewPasswordMismatch.Visible And _
            '                           Not Equals(cmbNewSecQues.Text.Length, 0) And Not Equals(txtNewSecAns.Text.Length, 0)
            GUIPopulator.AllowAccess(Me, eAccessType.Request, True)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.txtNewPassword_TextChanged")
        Finally
            'bPasswordMatched = Nothing
            'bIsPswdStrong = Nothing
            'dPswdStrength = Nothing
            '_BLMain = Nothing
            '_DLSettings = Nothing
        End Try
    End Sub

    Private Sub txtNewPasswordRetype_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNewPasswordRetype.TextChanged
        'Dim bPasswordMatched As Boolean = Equals(txtNewPassword.Text, txtNewPasswordRetype.Text)
        Try
            'picNewPasswordMismatch.Visible = Not bPasswordMatched
            'btnRequestAccess.Enabled = Not Equals(txtNewUserName.Text.Length, 0) And _
            '                           Not picExistingUser.Visible And Not picNewPasswordMismatch.Visible And _
            '                           Not Equals(cmbNewSecQues.Text.Length, 0) And Not Equals(txtNewSecAns.Text.Length, 0)
            GUIPopulator.AllowAccess(Me, eAccessType.Request, True)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.txtNewPasswordRetype_TextChanged")
        Finally
            'bPasswordMatched = Nothing
        End Try
    End Sub

    Private Sub cmbNewSecQues_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbNewSecQues.SelectedIndexChanged
        Try
            'Select Case cmbNewSecQues.SelectedIndex
            '    Case -1 : btnRequestAccess.Enabled = False
            '    Case Else : btnRequestAccess.Enabled = True
            'End Select
            'picNewSecQuesClear.Visible = Not Equals(cmbNewSecQues.SelectedIndex, -1)
            'btnRequestAccess.Enabled = Not Equals(txtNewUserName.Text.Length, 0) And _
            '                           Not picExistingUser.Visible And Not picNewPasswordMismatch.Visible And _
            '                           Not Equals(cmbNewSecQues.Text.Length, 0) And Not Equals(txtNewSecAns.Text.Length, 0)
            picNewSecQuesClear.Visible = Not Equals(cmbNewSecQues.SelectedIndex, -1)
            GUIPopulator.AllowAccess(Me, eAccessType.Request, True)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.cmbNewSecQues_SelectedIndexChanged")
        End Try
    End Sub

    Private Sub txtNewSecAns_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNewSecAns.TextChanged
        'Dim _BLMain As BusinessLogicLayer.BLMain
        Try
            '_BLMain = New BusinessLogicLayer.BLMain(BusinessLogicLayer.BLMain.eInitMode.Validation)
            'btnRequestAccess.Enabled = Not IsEmpty(txtNewSecAns.Text)
            'btnRequestAccess.Enabled = Not Equals(txtNewUserName.Text.Length, 0) And _
            '                           Not picExistingUser.Visible And Not picNewPasswordMismatch.Visible And _
            '                           Not Equals(cmbNewSecQues.Text.Length, 0) And Not Equals(txtNewSecAns.Text.Length, 0)
            GUIPopulator.AllowAccess(Me, eAccessType.Request, True)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.txtNewSecAns_TextChanged")
        Finally
            '_BLMain = Nothing
            'NO FINALLY
        End Try
    End Sub

    Private Sub picNewSecQuesClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picNewSecQuesClear.Click
        Try
            cmbNewSecQues.SelectedIndex = -1
            txtNewSecAns.Text = ""
            GUIPopulator.AllowAccess(Me, eAccessType.Request, True)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.picNewSecQuesClear_Click")
        End Try
    End Sub

    Private Sub btnRequestAccess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRequestAccess.Click
        Dim _BLMain As BusinessLogicLayer.BLMain
        Dim _DLSettings As DLSettings
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Dim sMessage As String
        Dim bValidUserDetails As Boolean = False

        Try
            _BLMain = New BusinessLogicLayer.BLMain(BusinessLogicLayer.BLMain.eInitMode.Validation)
            _DLSettings = New DLSettings
            _GUIPopulator = New EntertainmentManager.GUIPopulator

            Static iAllowedRequestAttempts As Integer = CInt(DLSettings.FetchOneSetting(dtSettingsMain, "UserMgmt", "AllowedRequestAttempts"))
            Static iRemainingAttempts As Integer = iAllowedRequestAttempts

            bValidUserDetails = Not _BLMain.IsExistingUser(txtNewUserName.Text, dtUserDetails) AndAlso _
                                _BLMain.CreateNewUser(txtNewUserName.Text, txtNewPassword.Text, cmbNewSecQues.Text, txtNewSecAns.Text)

            If Not bValidUserDetails Then
                bValidUserDetails = Nothing
                iRemainingAttempts -= 1

                If iRemainingAttempts < 0 Then
                    sMessage = "You have exceeded the permissible number of unsuccessful hits." & vbCrLf & _
                                   "Program will now exit.."
                    MessageBox.Show(sMessage, "Registration error..", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                    End
                End If

                sMessage = "User does not exists or wrong credentials entered!! " & _
                           "Login window will close after " & iAllowedRequestAttempts.ToString & " unsuccesful attempts (" & iRemainingAttempts.ToString & " remaining)." & vbCrLf & _
                           "Please use the ""Request Access"" or try again.."
                Select Case MessageBox.Show(sMessage, "Login error..", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                    Case Windows.Forms.DialogResult.Abort : End
                    Case Windows.Forms.DialogResult.Retry 'Do Nothing
                    Case Windows.Forms.DialogResult.Ignore
                        dHeight(0) = Me.Height : dHeight(1) = 223 : dWidth(0) = Me.Width : dWidth(1) = 436
                        _GUIPopulator.AnimateResize(Me)
                        '_GUIPopulator.HideAccessRequest(Me, True)
                        _GUIPopulator = Nothing
                End Select
            Else
                dHeight(0) = Me.Height : dHeight(1) = 223 : dWidth(0) = Me.Width : dWidth(1) = 436
                _GUIPopulator.AnimateResize(Me)
                '_GUIPopulator.HideAccessRequest(Me, True)
                _GUIPopulator = Nothing

                sMessage = "Your registration is pending for approval with administrator." & vbCrLf & _
                           "Once approved, enter your credentials to login.."
                MessageBox.Show(sMessage, "Login")
            End If

        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.btnRequestAccess_Click")
        Finally
            _BLMain = Nothing
            _DLSettings = Nothing
            _GUIPopulator = Nothing
            sMessage = Nothing
            bValidUserDetails = Nothing
        End Try
    End Sub

    Private Sub btnCancelRequest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelRequest.Click
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            _GUIPopulator = New EntertainmentManager.GUIPopulator
            pnlLogin.Enabled = True
            dHeight(0) = Me.Height : dHeight(1) = 223 : dWidth(0) = Me.Width : dWidth(1) = 436
            _GUIPopulator.AnimateResize(Me)
            '_GUIPopulator.HideAccessRequest(Me, True)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.btnCancelRequest_Click")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

#End Region

    Private Sub txtChngDefPswd_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtChngDefPswd.TextChanged
        'Dim _BLMain As BusinessLogicLayer.BLMain
        'Dim bPasswordMatched As Boolean = Equals(txtChngDefPswd.Text, txtChngDefPswdRetype.Text)
        'Dim bIsPswdStrong As Boolean = False
        'Dim dPswdStrength As Double = 0.0

        Try
            '_BLMain = New BusinessLogicLayer.BLMain(BusinessLogicLayer.BLMain.eInitMode.Validation)
            'bIsPswdStrong = _BLMain.IsPasswordStrong(txtChngDefPswd.Text, dPswdStrength)
            'progChngDefPswdStrength.Value = CInt(progChngDefPswdStrength.Minimum + dPswdStrength * (progChngDefPswdStrength.Maximum - progChngDefPswdStrength.Minimum))
            'picChngDefPswdMismatch.Visible = Not bPasswordMatched
            'btnChngDefPswdOK.Enabled = bPasswordMatched And bIsPswdStrong
            GUIPopulator.AllowAccess(Me, eAccessType.ChangeDefault, True)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.txtChngDefPswd_TextChanged")
        Finally
            '_BLMain = Nothing
            'bIsPswdStrong = Nothing
            'bPasswordMatched = Nothing
            'dPswdStrength = Nothing
        End Try
    End Sub

    Private Sub txtChngDefPswdRetype_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtChngDefPswdRetype.TextChanged
        'Dim _BLMain As BusinessLogicLayer.BLMain
        'Dim bPasswordMatched As Boolean = Equals(txtChngDefPswd.Text, txtChngDefPswdRetype.Text)
        'Dim bIsPswdStrong As Boolean = False
        'Dim dPswdStrength As Double = 0.0

        Try
            '_BLMain = New BusinessLogicLayer.BLMain(BusinessLogicLayer.BLMain.eInitMode.Validation)
            'bIsPswdStrong = _BLMain.IsPasswordStrong(txtChngDefPswd.Text, dPswdStrength)
            'picChngDefPswdMismatch.Visible = Not bPasswordMatched
            'btnChngDefPswdOK.Enabled = bPasswordMatched And bIsPswdStrong
            GUIPopulator.AllowAccess(Me, eAccessType.ChangeDefault, True)
        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.txtChngDefPswdRetype_TextChanged")
        Finally
            '_BLMain = Nothing
            'bIsPswdStrong = Nothing
            'bPasswordMatched = Nothing
            'dPswdStrength = Nothing
        End Try
    End Sub

    Private Sub btnChngDefPswdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChngDefPswdOK.Click
        Dim _BLMain As BusinessLogicLayer.BLMain
        Try
            _BLMain = New BusinessLogicLayer.BLMain(BusinessLogicLayer.BLMain.eInitMode.Validation)
            _BLMain.ChangeDefaultPassword(txtUserName.Text)
        Finally
            _BLMain = Nothing
        End Try
    End Sub

    Private Sub btnChngDefPswdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChngDefPswdCancel.Click
        Dim _BLMain As BusinessLogicLayer.BLMain
        Dim _DLReader As DLReader
        Dim _DLSettings As DLSettings
        Dim sMessage As String
        Dim dtUserDetails As DataTable
        Dim sUserName As String
        Dim iAllowedSkips As Integer

        Try
            _BLMain = New BusinessLogicLayer.BLMain(BusinessLogicLayer.BLMain.eInitMode.Validation)
            _DLReader = New DLReader("frmLogin")
            _DLSettings = New DLSettings
            dtUserDetails = New DataTable
            sUserName = txtUserName.Text

            iAllowedSkips = CInt(DLSettings.FetchOneSetting(dtSettingsMain, "UserMgmt", "AllowedChngDefSkips"))
            _DLReader.FetchUserDetails(dtUserDetails, "UserName = '" & sUserName & "'", "")
            If dtUserDetails.Select("NeedsPswdChng = " & iAllowedSkips.ToString).Length = 0 Then
                sMessage = "You have exceeded maximum number of allowed skips.." & vbCrLf & _
                         "You MUST change the default password!" & vbCrLf & vbCrLf & _
                         "Click 'Retry' to change default password or 'Cancel' to exit.."
                Select Case MessageBox.Show(sMessage, "Confirmation..", MessageBoxButtons.RetryCancel)
                    Case Windows.Forms.DialogResult.Retry 'Do Nothing
                    Case Windows.Forms.DialogResult.Cancel : End
                End Select
            Else
                sMessage = "Please change the default password at next login.." & vbCrLf & _
                         "(" & (iAllowedSkips - CInt(dtUserDetails.Rows(0).Item("NeedsPswdChng"))).ToString & "attempts remaining)"
                MessageBox.Show(sMessage)
                _BLMain.ChangeDefaultPasswordCancelled(sUserName) 'increment counter +1, starting at 1, MUST CHANGE if i=3
            End If

        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.btnChngDefPswdCancel_Click")
        Finally
            _BLMain = Nothing
            _DLReader = Nothing
            _DLSettings = Nothing
            sMessage = Nothing
            dtUserDetails = Nothing
            sUserName = Nothing
            iAllowedSkips = Nothing
        End Try
    End Sub

    Private Sub tmrAnimateControl_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrAnimateControl.Tick
        Static iTickCounter As Integer = 0
        Dim _GUIPopulator As EntertainmentManager.GUIPopulator
        Try
            iTickCounter += 1
            _GUIPopulator = New EntertainmentManager.GUIPopulator

            If iTickCounter >= iTickTransitions Then 'last tick
                'Reset counter and disable timer
                iTickCounter = 0
                tmrAnimateControl.Enabled = False

                'Change to final Height/Width and position to Centre Screen
                Me.Height = dHeight(1)
                Me.Width = dWidth(1)
                Me.StartPosition = FormStartPosition.CenterScreen

                'Allow exiting the application when expanded
                If dHeight(0) < dHeight(1) Or dWidth(0) < dWidth(1) Then 'increasing
                    Me.ControlBox = True
                ElseIf dHeight(0) > dHeight(1) Or dWidth(0) > dWidth(1) Then 'decreasing
                    Me.ControlBox = False
                End If

            ElseIf iTickCounter > 0 Then
                If iTickCounter = 1 Then _GUIPopulator.ResetAccessRequest(Me)
                If iTickCounter = 1 Then _GUIPopulator.ResetPasswordRetrieval(Me)
                Me.Height = Me.Height + CInt((dHeight(1) - dHeight(0)) / 50)
                Me.Width = Me.Width + CInt((dWidth(1) - dWidth(0)) / 50)
            End If

        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.tmrAnimateControl_Tick")
        Finally
            _GUIPopulator = Nothing
        End Try
    End Sub

    Private Sub tmrTransparency_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTransparency.Tick
        Static iTickCounter As Integer = 0
        Static iTransparencyChange As Integer = CInt(100 / iTickTransitions)

        Try
            iTickCounter += 1

            Select Case iFormEvents
                Case eFormEvents.Loading
                    Me.Opacity = 0
                    If iTickCounter >= iTickTransitions Then 'last tick
                        Me.Opacity = 100
                        tmrTransparency.Enabled = False
                    ElseIf iTickCounter > 0 Then
                        Me.Opacity += iTransparencyChange
                    End If

                Case eFormEvents.Closing
                    Me.Opacity = 100
                    If iTickCounter >= iTickTransitions Then 'last tick
                        Me.Opacity = 0
                        tmrTransparency.Enabled = False
                        Me.Close()
                    ElseIf iTickCounter > 0 Then
                        Me.Opacity -= iTransparencyChange
                    End If
            End Select
            lblUserName.Text = CStr(Me.Opacity)

        Catch ex As Exception
            ThrowBLException(ex, "EntertainmentManager.frmLogin.tmrTransparency_Tick")
        End Try
    End Sub

    Private Sub CloseMe()
        'Me.Hide()
        'frmEntertainmentMgr.Show()
        Me.Close()
        Exit Sub

        iFormEvents = eFormEvents.Closing
        tmrTransparency.Enabled = True
    End Sub





End Class
